package com.jeestudio.utils;

import org.apache.commons.codec.binary.Base64;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class AESProviderUtil {
    private static String AES_ECB_MODE = "AES/ECB/PKCS5Padding";
    private static String AES_CBC_MODE = "AES/CBC/PKCS5Padding";
    private static String AES_CFB_MODE = "AES/CFB/NOPADDING";

    public AESProviderUtil() {
    }

    public static String ECBDecrypt(String sSrc, String AESKEY) {
        try {
            byte[] raw = AESKEY.getBytes("utf-8");
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance(AES_ECB_MODE);
            cipher.init(2, skeySpec);
            byte[] encrypted1 = Base64.decodeBase64(sSrc);
            byte[] original = cipher.doFinal(encrypted1);
            String originalString = new String(original);
            return originalString;
        } catch (Exception var8) {
            return "";
        }
    }

    public static String ECBEncrypt(String sSrc, String sKey) {
        try {
            byte[] e = sKey.getBytes();
            SecretKeySpec skeySpec = new SecretKeySpec(e, "AES");
            Cipher cipher = Cipher.getInstance(AES_ECB_MODE);
            cipher.init(1, skeySpec);
            byte[] encrypted = cipher.doFinal(sSrc.getBytes());
            return Base64.encodeBase64String(encrypted);
        } catch (Exception var6) {
            var6.printStackTrace();
            return "";
        }
    }

    public static String CBCEncrypt(String sSrc, String AESKEY) {
        try {
            byte[] raw = AESKEY.getBytes("utf-8");
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance(AES_CBC_MODE);
            IvParameterSpec iv = new IvParameterSpec("0102030405060708".getBytes("utf-8"));
            cipher.init(1, skeySpec, iv);
            byte[] encrypted = cipher.doFinal(sSrc.getBytes("utf-8"));
            return Base64.encodeBase64String(encrypted);
        } catch (Exception var7) {
            return "";
        }
    }

    public static String CBCDecrypt(String sSrc, String sKey) {
        try {
            byte[] raw = sKey.getBytes("ASCII");
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance(AES_CBC_MODE);
            IvParameterSpec iv = new IvParameterSpec("0102030405060708".getBytes());
            cipher.init(2, skeySpec, iv);
            byte[] encrypted1 = Base64.decodeBase64(sSrc);
            byte[] original = cipher.doFinal(encrypted1);
            String originalString = new String(original);
            return originalString;
        } catch (Exception var9) {
            return "";
        }
    }

    public static String CFBEncrypt(String sSrc, String AESKEY) {
        try {
            byte[] raw = AESKEY.getBytes("utf-8");
            SecretKeySpec secretKeySpec = new SecretKeySpec(raw, "AES");
            IvParameterSpec paramSpec = new IvParameterSpec("0102030405060708".getBytes("utf-8"));
            Cipher ecipher = Cipher.getInstance(AES_CFB_MODE);
            ecipher.init(1, secretKeySpec, paramSpec);
            byte[] encrypted = ecipher.doFinal(sSrc.getBytes());
            return Base64.encodeBase64String(encrypted);
        } catch (Exception var7) {
            return "";
        }
    }

    public static String CFBDecrypt(String sSrc, String sKey) {
        try {
            byte[] raw = sKey.getBytes("ASCII");
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance(AES_CFB_MODE);
            IvParameterSpec iv = new IvParameterSpec("0102030405060708".getBytes());
            cipher.init(2, skeySpec, iv);
            byte[] encrypted1 = Base64.decodeBase64(sSrc);
            byte[] original = cipher.doFinal(encrypted1);
            String originalString = new String(original);
            return originalString;
        } catch (Exception var9) {
            return "";
        }
    }
}